/*
 * 作者:熊猫
 * 日期:2017 - 3 - 15 4 - 44 - 55
 *
 */

package net.lainiao.morethread.Chapter6.demo8;

import java.util.concurrent.atomic.AtomicIntegerArray;

/**
 * Created by Administrator on 2017/3/15.
 */
public class Main {
    public static void main(String[] args) {
        final int THREADS=100;
        AtomicIntegerArray array=new AtomicIntegerArray(1000);
        Incrementer incrementer=new Incrementer(array);
        Decrementer decrementer=new Decrementer(array);
        Thread[] addThreads=new Thread[THREADS];
        Thread[] removeThreads=new Thread[THREADS];
        for (int i = 0; i < THREADS; i++) {
            addThreads[i]=new Thread(incrementer);
            removeThreads[i]=new Thread(decrementer);
            addThreads[i].start();
            removeThreads[i].start();
        }
        for (int i = 0; i < THREADS; i++) {
            try {
                addThreads[i].join();
                removeThreads[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < array.length(); i++) {
            if(array.get(i)!=0){
                System.out.println("第"+i+"个元素为:"+array.get(i));
            }
        }
        System.out.println("Main Over");
    }
}
